package cn.pa;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

public class CompletableFutureBiolWater extends AbstractBoilWater {
    @Override
    public void make() throws Exception {
        System.out.println("make ThreadID:"+Thread.currentThread().getId());

        a_addWater();
        b_on();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
                c_boiling();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 1;
        });

//        //注册事件“监听”
//        future.whenComplete((v, e) -> {
//		    System.out.println("v:"+v);
//			System.out.println(e);
//            d_off();
//        });
        //异步通知类 任务执行完后调用accept 进行后续异步代码处理 通过对比主线程id可看出是否在主线程中执行
        // 测试得出CompletableFuture 异步执行的任务代码和执行完成后的通知处理代码都是在异步线程中 并不在当前主线程make ThreadID:1 BiConsumer.accept ThreadID:12 c_boiling ThreadID:12
        BiConsumer<Integer,Throwable> bic= new BiConsumer<Integer,Throwable>() {
            @Override
            public void accept(Integer o, Throwable o2) {
                System.out.println(o);
                System.out.println(o2);
                System.out.println("BiConsumer.accept ThreadID:"+Thread.currentThread().getId());
                d_off();
            }

            @Override
            public BiConsumer andThen(BiConsumer after) {
                return null;
            }
        };
        future.whenComplete(bic);
        while (!future.isDone()) {
            playGame();
        }
    }

  public static void main(String[] args) {
      try {
          new CompletableFutureBiolWater().make();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}
